// 深拷贝
export const deepClone = (target, cache = new WeakMap()) => {
  if (target === null || typeof target != 'object') {
    return target;
  }
  if (cache.get(target)) {
    return target;
  }
  const copy = Array.isArray(target) ? [] : {};
  cache.set(target, copy);
  Object.keys(target).forEach(
    (key: string | number) => (copy[key] = deepClone(target[key], cache)),
  );
  return copy;
};
// 广度优先遍历(只包括最深节点)
export function breadthFirstSearch(tree: any, callback: Fn) {
  const queue = [...tree];
  while (queue.length) {
    const node = queue.shift();
    if (node?.children) {
      queue.push(...node.children);
    } else {
      callback(node);
    }
  }
}
// 深度优先遍历（所有分支节点）
export function depthFirstSearch(data: Array<Object>, callback: Fn) {
  if (data) {
    data.map((item: any, key: number) => {
      if (item.children && item.children.length) {
        depthFirstSearch(item.children, callback);
      }
      callback(item, key);
    });
  }
}
